<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网络类型检测</title>
</head>
<body>
    <script>
        getNetworkType()
        function getNetworkType() {
            var ua = navigator.userAgent;
            var networkStr = ua.match(/NetType\/\w+/) ? ua.match(/NetType\/\w+/)[0] : 'NetType/other';
            networkStr = networkStr.toLowerCase().replace('nettype/', '');
            var networkType;
            switch(networkStr) {
                case 'wifi':
                    networkType = 'wifi';
                    break;
                case '4g':
                    networkType = '4G';
                    break;
                case '3g':
                    networkType = '3G';
                    break;
                case '3gnet':
                    networkType = '3G';
                    break;
                case '2g':
                    networkType = '2G';
                    break;
                default:
                    networkType = 'other';
            }
          alert(networkStr)
            console.log(networkStr)
        }
    </script>
</body>
</html>
